package main

import (
	"context"
	"crypto/md5"
	replicator "dts"
	"dts/config"
	"dts/mysql"
	"dts/sqltypes"
	"encoding/json"
	"flag"
	"fmt"
	"regexp"
	"strings"

	jsoniter "github.com/json-iterator/go"
)

var configPath = flag.String("config", "etc/replicator.ini", "path to replicator config file")

func main() {
	testJson()
	//TestAlter()
	return
	host := "4sframebusiness-dbproxy.xesv5.com:2150"
	table := "^xes_arris_4c(_[a-z]+)+_\\d+_\\d+$"
	res := strings.ContainsRune(table, '^')
	res1 := strings.ContainsRune(table, '*')
	res2 := strings.Contains(host, "dbproxy")

	fmt.Println(res)
	fmt.Println(res1)
	fmt.Println(res2)
	return
	updateHash()
	return

	var err error
	config.Conf, err = config.LoadConfig(*configPath)
	if err != nil {
		fmt.Printf("load config error: %v\n", err.Error())
		return
	}

	var params = &mysql.ConnParams{
		Host:  "10.90.102.48",
		Port:  3306,
		Uname: "xes_root",
		Pass:  "XueErSiroot123",
	}

	stats := replicator.NewStats()
	storage := replicator.NewDBClient(params, stats)
	err = storage.Connect()
	if err != nil {
		fmt.Println(err.Error())
	}

	//replicator.CopyHistoryLog.Insert(storage, context.TODO(), 1,"infra_zeus","user")
	//replicator.CopyHistoryLog.Update(storage, 3,5)
	data, _ := replicator.CopyHistoryLog.GetCnt(storage, 11, "test_xes_arris2", "xes_arris_stu_1_21")

	fmt.Println(data)

}

func testTables() {
	addr := "10.90.102.48:8080"
	stats := replicator.NewStats()
	_, err := replicator.LoadFromDB(1, stats, addr)

	if err != nil {

	}

}

func testHash() {
	str := "123456"
	//方法一
	data := []byte(str)
	has := md5.Sum(data)
	md5str1 := fmt.Sprintf("%x", has) //将[]byte转成16进制
	fmt.Println(md5str1)

}

type Student struct {
	name string
}

func testJson() {
	var json2 = jsoniter.ConfigCompatibleWithStandardLibrary
	stu := Student{name: "123"}
	v, err := json2.Marshal(stu)
	if err != nil {

	}
	fmt.Println(v)
	v1, _ := json.Marshal(stu)
	fmt.Println(v1)
}

func updateHash() {
	var params = &mysql.ConnParams{
		Host:  "10.90.73.168",
		Port:  3306,
		Uname: "root",
		Pass:  "123456",
	}

	stats := replicator.NewStats()
	dbClient := replicator.NewDBClient(params, stats)
	err := dbClient.Connect()
	if err != nil {
		fmt.Println(err.Error())
	}

	result, err := dbClient.ListTasks(context.TODO(), 200, 1, "")

	if err != nil {
		fmt.Println(err.Error())
	}
	rowsToTasks(dbClient, result.Rows)
	if err != nil {
	}
}

func testGho() {
	str := "/^_.*(_ghc|_gho|_del)$/"

	table1 := strings.Trim(str, "/")
	fmt.Println(table1)
	re, _ := regexp.Compile(table1)
	ok := re.MatchString("_student_ghc")
	ok1 := re.MatchString("_student_gho")
	ok2 := re.MatchString("_student_del")
	ok3 := re.MatchString("_xes_arris_stu_1_119_ghc")
	ok4 := re.MatchString("xes_arris_stu_1_119")

	fmt.Println(ok)
	fmt.Println(ok1)
	fmt.Println(ok2)
	fmt.Println(ok3)
	fmt.Println(ok4)
}

func rowsToTasks(dbClient *replicator.DBClient, rows [][]sqltypes.Value) {
	for _, row := range rows {
		id, err := row[0].ToInt64()
		fmt.Println(id)
		if err != nil {
			continue
		}

		sql := fmt.Sprintf("update replicator.replication set hash= '%d' where id = %d", id, id)
		fmt.Println(sql)

		dbClient.Execute(sql)
	}
}
